﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Tanmia.Core;

namespace Tanmia.Controls
{
    /// <summary>
    /// Interaction logic for ContactDetailTool.xaml
    /// </summary>
    public partial class ContactDetailTool : UserControl
    {
        public static readonly DependencyProperty ContactDetailsProperty = DependencyProperty.Register("ContactDetails", typeof(List<ContactDetail>), typeof(ContactDetailTool), new FrameworkPropertyMetadata(new List<ContactDetail>()));

        public List<ContactDetail> ContactDetails
        {
            get { return (List<ContactDetail>)GetValue(ContactDetailsProperty); }
            set
            {
                SetValue(ContactDetailsProperty, value);

                if (value.Count == 0)
                {
                    lstContactDetail.ItemsSource = null;
                    lstContactDetail.ItemsSource = ContactDetails;
                }
            }
        }

        public ContactDetailTool()
        {
            InitializeComponent();

            btnSave.Content = "جديد";

            cmbType.SelectedIndex = 0;

            cmbType.Items.Add("هاتف");
            cmbType.Items.Add("هاتف محمول");
            cmbType.Items.Add("فاكس");
            cmbType.Items.Add("عنوان");
            cmbType.Items.Add("بريد إلكتروني");
            cmbType.Items.Add("آخر");
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (btnSave.Content == "جديد" as object)
            {
                cmbType.IsEnabled = true;
                txtValue.IsEnabled = true;
                txtNote.IsEnabled = true;
                lblNote.IsEnabled = true;
                lblType.IsEnabled = true;
                lblValue.IsEnabled = true;
                btnDelete.IsEnabled = false;
                btnSave.Content = "حفظ";
                cmbType.Focus();
            }
            else
            {
                if (string.IsNullOrEmpty(txtValue.Text))
                {
                    txtValue.Focus();
                    return;
                }

                if (ContactDetails != null)
                    ContactDetails.Add(new ContactDetail() { ContactDetailType = (ContactDetailTypeEnum)cmbType.SelectedIndex, CreateDate = DateTime.Now, Note = txtNote.Text, Value = txtValue.Text });

                cmbType.IsEnabled = false;
                txtValue.IsEnabled = false;
                txtNote.IsEnabled = false;
                lblNote.IsEnabled = false;
                lblType.IsEnabled = false;
                lblValue.IsEnabled = false;
                btnDelete.IsEnabled = true;

                btnSave.Content = "جديد";

                lstContactDetail.ItemsSource = null;
                lstContactDetail.ItemsSource = ContactDetails;
            }

            cmbType.SelectedIndex = 0;
            txtValue.Text = "";
            txtNote.Text = "";
        }

        private void lstContactDetail_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbType.IsEnabled == true)
            {
                cmbType.IsEnabled = false;
                txtValue.IsEnabled = false;
                txtNote.IsEnabled = false;
                lblNote.IsEnabled = false;
                lblType.IsEnabled = false;
                lblValue.IsEnabled = false;
                btnDelete.IsEnabled = true;

                cmbType.SelectedIndex = 0;
                txtValue.Text = "";
                txtNote.Text = "";

                btnSave.Content = "جديد";
            }

            try
            {
                cmbType.SelectedIndex = (int)((lstContactDetail.SelectedItem as ContactDetail).ContactDetailType);
                txtNote.Text = (lstContactDetail.SelectedItem as ContactDetail).Note;
                txtValue.Text = (lstContactDetail.SelectedItem as ContactDetail).Value;
            }
            catch { }

        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (lstContactDetail.SelectedItem != null)
            {
                int x = lstContactDetail.SelectedIndex;

                ContactDetails.Remove(lstContactDetail.SelectedItem as ContactDetail);

                lstContactDetail.ItemsSource = null;
                lstContactDetail.ItemsSource = ContactDetails;
                if (x == 0)
                    lstContactDetail.SelectedIndex = x;
                else
                    lstContactDetail.SelectedIndex = x - 1;

                if (lstContactDetail.Items.Count == 0)
                {
                    btnDelete.IsEnabled = false;
                }
                else
                {
                    btnDelete.IsEnabled = true;
                }
            }
        }

        private void lstContactDetail_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Delete)
                if (lstContactDetail.SelectedItem != null)
                {
                    int x = lstContactDetail.SelectedIndex;

                    ContactDetails.Remove(lstContactDetail.SelectedItem as ContactDetail);

                    lstContactDetail.ItemsSource = null;
                    lstContactDetail.ItemsSource = ContactDetails;
                    if (x == 0)
                        lstContactDetail.SelectedIndex = x;
                    else
                        lstContactDetail.SelectedIndex = x - 1;

                    if (lstContactDetail.Items.Count == 0)
                    {
                        btnDelete.IsEnabled = false;
                    }
                    else
                    {
                        btnDelete.IsEnabled = true;
                    }
                }
        }

        private void cmbType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            txtValue.Focus();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            lstContactDetail.ItemsSource = ContactDetails;

            if (lstContactDetail.Items.Count == 0)
            {
                btnDelete.IsEnabled = false;
            }
        }
    }
}
